﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetAppBlocks.Forms;
using ProgNetComponents.Data;

namespace MiniKsiegowosc.Wrk.Ksiegi.KPIR
{
    public partial class FormWrkKpir : ProgNetSpreadSheetForm
    {
        public FormWrkKpir()
        {
            InitializeComponent();
        }
        public FormWrkKpir(int rok) : this()
        {
            CurrentRecordId = rok;
        }

        protected override void FinishEdit()
        {
            base.FinishEdit();
            DataRow[] rows = wrkKpirDataSet.WRK_KPIR.Select();
            foreach (DataRow row in rows)
            {
                row.SetColumnValue("WRK_KPIR_ROK", row["WRK_KPIR_DATA"].AsDateTime().Year);
            }
        }
        protected override ProgNetComponents.Data.ObjectKeyValuePair[] GetOpenParameters()
        {
            ObjectKeyValuePair k = new ObjectKeyValuePair("@ROK", CurrentRecordId);
            return new ObjectKeyValuePair[] { k };
        }
        protected override string FormText
        {
            get
            {
                return base.FormText + " Rok: " + CurrentRecordId.AsString();
            }
        }
        private void gvMain_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column == colWRK_KPIR_DATA)
            {
                object current = e.Value;
                if (current == null)
                    gvMain.SetRowCellValue(e.RowHandle, colMiesiac, -1);
                else
                {
                    gvMain.SetRowCellValue(e.RowHandle, colMiesiac, current.AsDateTime().Month);
                }
            }
        }

        private void BrowserSettings_AdditionalBrowserValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            e.Cancel = true;
            DataRow r = gvMain.GetFocusedDataRow();
            WrkTools.AddKpirFromPredef(wrkKpirDataSet, e.NewValue.AsInt(), r);
        }

        List<DataRow> list = new List<DataRow>();
        protected override bool ValidateRow(DataRow r, DevExpress.XtraGrid.Views.Grid.GridView gv)
        {
            bool result = base.ValidateRow(r, gv);
            if (gv == gvMain)
            {
                if (r["WRK_KPIR_DATA"].IsNotNull())
                {
                    DateTime d = r["WRK_KPIR_DATA"].AsDateTime();
                    if (d.Year != CurrentRecordId.AsInt())
                    {
                        list.Add(r);
                    }
                    if (!WrkTools.IsRokValid(d.Year))
                    {
                        r.SetColumnError("WRK_KPIR_DATA", "Brak Roku Księgowego.");
                        AddError("Brak Roku Księgowego " + d.Year);
                        result = false;
                    }
                }
            }
            return result;
        }

        protected override void ValidateAll()
        {
            list.Clear();
            base.ValidateAll();
           
            if (list.Count > 0)
            {
                if (this.ShowDxYNQuestionDialog("Nie wszystkie wpisy są z roku " + CurrentRecordId.AsString() + ".\nChcesz zapisać?") != System.Windows.Forms.DialogResult.Yes)
                {
                    AddError("Nie wszystkie wpisy są z roku " + CurrentRecordId.AsString());
                    foreach (DataRow r in list)
                    {
                        r.SetColumnError("WRK_KPIR_DATA", "Zły rok.");
                    }
                }
            }
            
            foreach (DataRow r in list)
                gvMain.ExpandRow(r);
        }

        private void bbiDrukuj_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RaportTools.FormMonthRaportPrint.ShowPrintDialog(CurrentRecordId.AsInt(), new raportKpir());
        }
        
    }
}
